#coding:utf-8
# -*- coding: utf-8 -*-

import datetime,time
from pytz import timezone

#日志模块
from utils import logutils
logger=logutils.getlogger(__file__)
cst_tz = timezone('Asia/Shanghai')


def now2str():
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#时间字符串转datetime
def str2dt(datetimestr):
    dt=datetime.datetime.strptime(datetimestr,"%Y-%m-%d %H:%M:%S")
    return dt


#时间戳转时间字符串
def timestamp2strftime(timeStamp):
    cst_tz = timezone('Asia/Shanghai')
    # dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    dateArray = datetime.datetime.fromtimestamp(timeStamp).replace(tzinfo=cst_tz)
    otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    logger.debug(otherStyleTime)
    return otherStyleTime
#时间戳转时间字符串
def timestamp2strftime1(timeStamp):
    cst_tz = timezone('Asia/Shanghai')
    # dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    dateArray = datetime.datetime.fromtimestamp(timeStamp).replace(tzinfo=cst_tz)
    otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
    logger.debug(otherStyleTime)
    return otherStyleTime
#当前时间转换为1970秒数
def now2seconds():
    # timeDateStr = "2014-07-29 00:00:00"
    # time1 = datetime.datetime.strptime(timeDateStr, "%Y-%m-%d %H:%M:%S")
    now=datetime.datetime.now()
    secondsFrom1970 = time.mktime(now.timetuple())
    return secondsFrom1970

def ses2datetime(sec):
    timeArray = time.localtime(sec)  # 1970秒数
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    dt = datetime.datetime.strptime(otherStyleTime, "%Y-%m-%d %H:%M:%S")
    return dt


def nowdt2str():
    dtnow=datetime.datetime.now()
    dtstr=dtnow.strftime("%Y%m%d%H%M%S%f")
    return dtstr



def nowdt2str2():
    dtnow=datetime.datetime.now()
    dtstr=dtnow.strftime("%Y-%m-%d")
    return dtstr
def now2ymd():
    dtnow=datetime.datetime.now()
    dtstr=dtnow.strftime("%Y%m%d")
    return dtstr

def nowTimestampStr():
    dtnow=datetime.datetime.now()
    dtstr=dtnow.strftime("%Y-%m-%d %H:%M:%S.%f")
    return dtstr
#毫秒时间戳
def currentTimeMillis():
    t = time.time()
    return int(round(t * 1000))
#纳秒时间戳
def currentTimeNano():
    t = time.time()
    return int(round(t * 1000000))

if __name__=='__main__':
    print(time.time())
    current = currentTimeMillis()
    print(current)
    tmp=currentTimeNano()
    print(tmp)
    tmp=timestamp2strftime1(current/1000)
    print(tmp)
